package dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import beans.Restaurant;
import beans.RestaurantTag;

public class RestaurantTagDAO {
	
private HibernateTemplate hibernateTemplate;

	
public void setSessionFactory(SessionFactory sessionFactory){
	this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public void saveRestaurantTag (RestaurantTag restaurantTag){
	this.hibernateTemplate.save(restaurantTag);
}
public List<RestaurantTag> GetAllRestaurantTag(){
	return hibernateTemplate.loadAll(RestaurantTag.class);
}
public RestaurantTag getTagsByRestaurantAndType(Restaurant restaurant, String type){
	RestaurantTag restaurantTag = new RestaurantTag();
	for(RestaurantTag x : GetAllRestaurantTag()){
		
		if((x.getRestaurant_id()==restaurant.getId()&&(x.getType().equalsIgnoreCase(type)))){
			restaurantTag.setId(x.getId());
			restaurantTag.setRestaurant_id(x.getRestaurant_id());
			restaurantTag.setTag(x.getTag());
			restaurantTag.setType(x.getType());
		}
		
	}
	return restaurantTag;
}
public void updateRestaurantTag (RestaurantTag restaurantTag){
	this.hibernateTemplate.update(restaurantTag);
}

public String GetAllTagsFromRestaurant(Restaurant restaurant){
	String result = null;
	for (RestaurantTag x: GetAllRestaurantTag() ) {
		if(x.getRestaurant_id()== restaurant.getId()) {
			if(x.getTag().length()==0) continue;
			if(result==null){
				result=x.getTag();
			}
			else{
				result=result+", "+x.getTag();
			}
		}
	}
	return result;
	}


}
